package org.dromara.basic.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.dromara.basic.domain.bo.MemberBo;
import org.dromara.basic.domain.vo.MemberImportVo;
import org.dromara.basic.domain.vo.MemberVo;
import org.dromara.basic.domain.vo.TeachClassVo;
import org.dromara.basic.service.IMemberService;
import org.dromara.basic.service.ITeachClassService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.core.validate.OtherGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.utils.FileUploadUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 成员信息表
 *
 * @author chl
 * @date 2024-08-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/basic/member")
public class MemberController extends BaseController {

    private final IMemberService memberService;

    private final ITeachClassService iTeachClassService;

    /**
     * 查询成员信息表列表
     */
    @SaCheckPermission("basic:member:list")
    @GetMapping("/list")
    public TableDataInfo<MemberVo> list(MemberBo bo, PageQuery pageQuery) {
        return memberService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取成员信息表列表详细信息
     *
     * @param bo 查询主体
     */
    @SaCheckPermission("basic:member:query")
    @GetMapping("/infoList")
    public R<List<MemberVo>> getInfoList(MemberBo bo) {
        return R.ok(memberService.queryList(bo));
    }

    /**
     * 导出成员信息表列表
     */
    @SaCheckPermission("basic:member:export")
    @Log(title = "成员信息表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MemberBo bo, HttpServletResponse response) {
        List<MemberVo> list = memberService.queryList(bo);
        AtomicInteger index = new AtomicInteger(1);
        list.forEach(vo-> {
            // 序号递增
            vo.setIndex(index.getAndIncrement());
        });
        ExcelUtil.exportExcel(list, "成员信息表", MemberVo.class, response);
    }
    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "用户信息导入模板", MemberImportVo.class, response);
    }
    /**
     * 获取成员信息表详细信息
     *
     * @param memberId 主键
     */
    @SaCheckPermission("basic:member:query")
    @GetMapping("/{memberId}")
    public R<MemberVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long memberId) {
        return R.ok(memberService.queryById(memberId));
    }


    @SaCheckPermission("basic:member:add")
    @Log(title = "成员信息表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<T> addInfo(@Validated(OtherGroup.class) @RequestBody  MemberBo bo) {
        Boolean aBoolean = memberService.insertByBo(bo, true);
        if(!aBoolean){
            return R.fail("添加失败");
        }
        return R.ok("添加成功");
    }
    /**
     * 修改成员信息表
     */
    @SaCheckPermission("basic:member:edit")
    @Log(title = "成员信息表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/edit")
    public R<Void> editInfo(@Validated(OtherGroup.class) @RequestBody MemberBo bo) {
        Boolean aBoolean = memberService.updateByBo(bo, true);
        if(!aBoolean){
            return R.fail("添加失败");
        }
        return R.ok("添加成功");
    }


    /**
     * 删除成员信息表
     *
     * @param memberIds 主键串
     */
    @SaCheckPermission("basic:member:remove")
    @Log(title = "成员信息表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{memberIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] memberIds) {
        return toAjax(memberService.deleteWithValidByIds(List.of(memberIds), true));
    }


}
